package com.listtest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.utility.ImageLoader;
import com.utility.JSONParser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class ListActivityMain extends Activity   {
	
	
	private static LayoutInflater inflater=null;
	
	//URL to get JSON Array
    private static String url = "http://202.21.149.53/Gecko/api/waterleak/";
    
  //JSON Node Names 
     static final String TAG_ID = "Id";
     static final String TAG_TOPIC = "Topic";
     static final String TAG_Detail = "Detail";
     static final String TAG_DURATION = "Duration";
     static final String TAG_IMAGE = "Image";

     public ArrayList<HashMap<String, String>> result;
     public ListView list;
     public ListAdapter adapter;
     
     private static final int SWIPE_MIN_DISTANCE = 120;
 	 private static final int SWIPE_MAX_OFF_PATH = 250;
 	 private static final int SWIPE_THRESHOLD_VELOCITY = 100;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		    
	
        
		
	   result=new ArrayList<HashMap<String, String>>();
		
		// Getting JSON from URL
		JSONParser jParser = new JSONParser();
		JSONObject json = jParser.getJSONFromUrl(url);
		Log.d("Return JSON : ",json.toString());
		try {
			JSONArray jArray = json.getJSONArray("Data");
			
			for(int i=0;i<jArray.length();i++){
			
			HashMap<String, String> map = new HashMap<String, String>();
			JSONObject json_data = jArray.getJSONObject(i);
			map.put(TAG_ID, json_data.getString("Id"));
			map.put(TAG_TOPIC, json_data.getString("Topic"));
			map.put(TAG_Detail, json_data.getString("Detail"));
			map.put(TAG_DURATION, "");
			map.put(TAG_IMAGE, "");
			
			
			result.add(map);
			
			}
			
		}
		
	    catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				 
			}
			
			list=(ListView)findViewById(R.id.list);
       	    adapter=new ListAdapter(this,result);        
            list.setAdapter(adapter); 
            //adapter.notifyDataSetChanged();
            
            
        
            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {
     
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
                    
                	
					Intent i = new Intent();
					i.setClass(ListActivityMain.this, SampleActivity.class);

					// parameters
					i.putExtra("id", result.get(position).get(TAG_ID));
					i.putExtra("topic", result.get(position).get(TAG_TOPIC));
					i.putExtra("detail", result.get(position).get(TAG_Detail));

					// start the sample activity
					startActivity(i);
     
                }
            });
		
	}

}
